<html>
<head>
    <title>Web Socket Test</title>
</head>
<body>
<script type="text/javascript">
    var socket;
    if (!window.WebSocket) {
        window.WebSocket = window.MozWebSocket;
    }
    if (window.WebSocket) {
        socket = new WebSocket("ws://localhost:2048/ws");
        socket.onmessage = function(event) {
            var ta = document.getElementById('responseText');
            ta.value = ta.value + '\n' + event.data
        };
        socket.onopen = function(event) {
            var ta = document.getElementById('responseText');
            ta.value = "连接到服务器！";
        };
        socket.onclose = function(event) {
            var ta = document.getElementById('responseText');
            ta.value = ta.value + "从服务器退出！";
        };
    } else {
        alert("服务器连接失败（大概是你的浏览器版本太低）");
    }

    function send(message) {
        if (!window.WebSocket) { return; }
        if (socket.readyState == WebSocket.OPEN) {
            socket.send(message);
        } else {
            alert("连接丢失");
        }
    }
</script>
<form onsubmit="return false;">
    <h1>↓聊天室↓</h1>
    <textarea id="responseText" style="width: 500px; height: 300px;"></textarea>
    <h1></h1>
    <input type="text" name="message" value="Hello, World!"><input
        type="button" value="发送"
        onclick="send(this.form.message.value)">
</form>
</body>
</html>